Imports Phoenix.DataAccess
Imports Phoenix.Helpers

Namespace Forms.Dialogs
    Public Class NewMasterLevelDialog : Inherits PhoenixFormBase

#Region " Windows Form Designer generated code "

        Public Sub New()
            MyBase.New()

            'This call is required by the Windows Form Designer.
            InitializeComponent()

            'Add any initialization after the InitializeComponent() call
        End Sub

        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents OkButton As DevExpress.XtraEditors.SimpleButton
        Friend WithEvents NameTextEdit As DevExpress.XtraEditors.TextEdit
        Friend WithEvents ParentComboBoxEdit As DevExpress.XtraEditors.ComboBoxEdit
        Friend WithEvents Label1 As System.Windows.Forms.Label
        Friend WithEvents Label2 As System.Windows.Forms.Label
        Friend WithEvents CancelBtn As DevExpress.XtraEditors.SimpleButton
        Friend WithEvents ErrorProvider As System.Windows.Forms.ErrorProvider
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.NameTextEdit = New DevExpress.XtraEditors.TextEdit
            Me.ParentComboBoxEdit = New DevExpress.XtraEditors.ComboBoxEdit
            Me.OkButton = New DevExpress.XtraEditors.SimpleButton
            Me.CancelBtn = New DevExpress.XtraEditors.SimpleButton
            Me.Label1 = New System.Windows.Forms.Label
            Me.Label2 = New System.Windows.Forms.Label
            Me.ErrorProvider = New System.Windows.Forms.ErrorProvider
            CType(Me.NameTextEdit.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.ParentComboBoxEdit.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            Me.SuspendLayout()
            '
            'NameTextEdit
            '
            Me.NameTextEdit.EditValue = ""
            Me.NameTextEdit.Location = New System.Drawing.Point(72, 8)
            Me.NameTextEdit.Name = "NameTextEdit"
            '
            'NameTextEdit.Properties
            '
            Me.NameTextEdit.Properties.ValidateOnEnterKey = True
            Me.NameTextEdit.Size = New System.Drawing.Size(136, 20)
            Me.NameTextEdit.TabIndex = 1
            '
            'ParentComboBoxEdit
            '
            Me.ParentComboBoxEdit.EditValue = ""
            Me.ParentComboBoxEdit.Location = New System.Drawing.Point(72, 40)
            Me.ParentComboBoxEdit.Name = "ParentComboBoxEdit"
            '
            'ParentComboBoxEdit.Properties
            '
            Me.ParentComboBoxEdit.Properties.Buttons.AddRange(New DevExpress.XtraEditors.Controls.EditorButton() {New DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)})
            Me.ParentComboBoxEdit.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor
            Me.ParentComboBoxEdit.Size = New System.Drawing.Size(136, 20)
            Me.ParentComboBoxEdit.TabIndex = 2
            '
            'OkButton
            '
            Me.OkButton.Location = New System.Drawing.Point(240, 8)
            Me.OkButton.Name = "OkButton"
            Me.OkButton.TabIndex = 3
            Me.OkButton.Text = "&OK"
            '
            'CancelBtn
            '
            Me.CancelBtn.Location = New System.Drawing.Point(240, 40)
            Me.CancelBtn.Name = "CancelBtn"
            Me.CancelBtn.TabIndex = 4
            Me.CancelBtn.Text = "&Cancel"
            '
            'Label1
            '
            Me.Label1.Location = New System.Drawing.Point(8, 8)
            Me.Label1.Name = "Label1"
            Me.Label1.Size = New System.Drawing.Size(48, 23)
            Me.Label1.TabIndex = 4
            Me.Label1.Text = "Name :"
            '
            'Label2
            '
            Me.Label2.Location = New System.Drawing.Point(8, 40)
            Me.Label2.Name = "Label2"
            Me.Label2.Size = New System.Drawing.Size(48, 23)
            Me.Label2.TabIndex = 5
            Me.Label2.Text = "Parent :"
            '
            'ErrorProvider
            '
            Me.ErrorProvider.ContainerControl = Me
            '
            'NewMasterLevelDialog
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(322, 70)
            Me.Controls.Add(Me.Label2)
            Me.Controls.Add(Me.Label1)
            Me.Controls.Add(Me.CancelBtn)
            Me.Controls.Add(Me.OkButton)
            Me.Controls.Add(Me.ParentComboBoxEdit)
            Me.Controls.Add(Me.NameTextEdit)
            Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
            Me.MaximizeBox = False
            Me.MinimizeBox = False
            Me.Name = "NewMasterLevelDialog"
            Me.ShowInTaskbar = False
            Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
            Me.Text = "New Master Level"
            CType(Me.NameTextEdit.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.ParentComboBoxEdit.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            Me.ResumeLayout(False)

        End Sub

#End Region

#Region " Data "
        Private mML As DataSetData.MasterLevel
        Public Property MasterLevel() As DataSetData.MasterLevel
            Get
                Return mML
            End Get
            Set(ByVal Value As DataSetData.MasterLevel)
                mML = Value
            End Set
        End Property


        Private Function DoNew() As Boolean
            If Not (ValidateName() And ValidateParent()) Then Return False
            AppDataLayer.DoInTransaction(AddressOf MakeMasterLevel)
            Return True
        End Function

        Private Sub MakeMasterLevel()
            MasterLevelHelper.CreateMasterLevel(NameTextEdit.Text, AppDataLayer.MasterLevels(ParentComboBoxEdit.SelectedIndex))
        End Sub
#End Region

#Region " Event Handlers "
        Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim index As Integer = -1
            For Each ml As DataAccess.DataSetData.MasterLevel In AppDataLayer.MasterLevels
                If ml Is MasterLevel Then
                    index = ParentComboBoxEdit.Properties.Items.Add(ml.LevelName)
                Else
                    ParentComboBoxEdit.Properties.Items.Add(ml.LevelName)
                End If
            Next
            If index <> -1 Then ParentComboBoxEdit.SelectedIndex = index
        End Sub

        Private Sub OkButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OkButton.Click
            If Not (ValidateName() And ValidateParent()) Then Return

            AppDataLayer.DoInTransaction(AddressOf MakeMasterLevel)

            Close()
        End Sub

        Private Sub CancelButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CancelBtn.Click
            Close()
        End Sub

        Private Sub NameTextEdit_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles NameTextEdit.KeyPress
            If e.KeyChar = vbCr Then
                OkButton_Click(OkButton, EventArgs.Empty)
            End If
        End Sub

#End Region

#Region " Validation "
        Private Sub ParentComboBoxEdit_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ParentComboBoxEdit.Validating
            ValidateParent()
        End Sub

        Private Sub NameTextEdit_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles NameTextEdit.Validating
            ValidateName()
        End Sub

        Private Function ValidateName() As Boolean
            If NameTextEdit.Text.Trim = "" Then
                ErrorProvider.SetError(NameTextEdit, Res.MasterLevelEmptyNameError)
                Return False
            Else
                ErrorProvider.SetError(NameTextEdit, String.Empty)
                Return True
            End If
        End Function

        Private Function ValidateParent() As Boolean
            If ParentComboBoxEdit.SelectedIndex = -1 Then
                ErrorProvider.SetError(ParentComboBoxEdit, Res.MasterLevelNoParentError)
                Return False
            Else
                ErrorProvider.SetError(ParentComboBoxEdit, String.Empty)
                Return True
            End If
        End Function

#End Region

    End Class

End Namespace
